<?php

if (function_exists('add_theme_support')) {
    add_theme_support('post-thumbnails');
}
// Set path to Slope Framework and theme specific functions
$admin_path = TEMPLATEPATH . '/admin-functions/';
$include_path = TEMPLATEPATH . '/stInclude/';
require_once $include_path . '/libs/functions/core.php';
require_once $include_path . '/libs/classes/stthemehook.php';
require_once $include_path . '/libs/classes/stthemefunctions.php';
require_once $include_path . '/libs/classes/stthememodel.php';
require_once $include_path . '/libs/classes/stthemeview.php';
require_once $include_path . '/libs/classes/stwidgets.php';

// Add hook function
$config = include $include_path . 'config.php';
if (is_admin()) {
    require_once ($admin_path . 'admin.php');
} else {
    require_once $admin_path . 'hook.php';
    $hook = new hook($config);
}
do_action($config['theme_name'] . '_extra_contruction');

function my_get_image_url($imageID, $type = '', $picturepath = '', $fileName = '') {
    global $wpdb;

    // safety first
    $imageID = (int) $imageID;

    // get gallery values
    if (empty($fileName)) {
        list($fileName, $picturepath ) = $wpdb->get_row("SELECT p.filename, g.path FROM $wpdb->nggpictures AS p INNER JOIN $wpdb->nggallery AS g ON (p.galleryid = g.gid) WHERE p.pid = '$imageID' ", ARRAY_N);
    }

    if (empty($picturepath)) {
        $picturepath = $wpdb->get_var("SELECT g.path FROM $wpdb->nggpictures AS p INNER JOIN $wpdb->nggallery AS g ON (p.galleryid = g.gid) WHERE p.pid = '$imageID' ");
    }

    // set gallery url
    if ($type != '') {
        $imageURL = site_url() . '/' . $picturepath . '/' . $type . '/thumbs_' . $fileName;
    } else {
        $imageURL = site_url() . '/' . $picturepath . '/' . $fileName;
    }

    return $imageURL;
}

function my_query_vars($vars) {
    // these values should match those in the rewrite rule query string above
    // I recommend using something more unique than 'action' and 'show', as you
    // could collide with other plugins or WordPress core
    $my_vars = array(
        'page',
    );

    return array_merge($my_vars, $vars);
}

//add_filter('query_vars', 'my_query_vars');


function my_get_home_path() {
	$home = get_option( 'home' );
	$siteurl = get_option( 'siteurl' );
	if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
		$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
		$pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
		$home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
		$home_path = trailingslashit( $home_path );
	} else {
		$home_path = ABSPATH;
	}

	return str_replace( '\\', '/', $home_path );
}

// get all client for hd player video

function getHDClient(){
    $data =  array(array('client_id'=> 1, 'name'=> 'Client 1'),array('client_id'=> 2, 'name'=> 'Client 2') ); 
    return $data; 
}

function get_client_name($client_id){
    return 'Client '.$client_id; 
}

add_action( 'wp_login_failed', 'my_front_end_login_fail' );  // hook failed login

function my_front_end_login_fail( $username ) {
   $referrer = home_url('dang-nhap');  // where did the post submission come from?
   // if there's a valid referrer, and it's not the default log-in screen
   if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
      wp_redirect( $referrer . '?login=failed' );  // let's append some information (login=failed) to the URL for the theme to use
      exit;
   }
}
